#!/usr/bin/perl

use Getopt::Long "GetOptions";

$ENV{"LC_ALL"} = "C";

# Import global variables
#require "vaanee.config";

use vaaneeConfig qw(%C);
#use vaaneeConfigtrain qw(%C);
#use loadfeaturefiles.AlignExp3 qw(%dbs %C); # For Alignment Algorithm 4
#use VaaneeDecode.AlignExp3 qw(%dbs %C);	# For Alignment Algorithm 4
use loadfeaturefiles qw(%dbs %C);
use VaaneeDecodeFastPerm qw(%dbs %C);  # For Decoding Algorithms

&GetOptions('mode=s' => \$mode,
	    's=s' => \$srcFile,
	    't=s' => \$tgtFile,
	    'd=s' => \$srcDep,
	    'a=s' => \$alignFile,
	    'z=s' => \$dictFile,
	    'nz=s' => \$numDict
);


if(!$mode) {
	die "-mode train/test/decode\n";
}
elsif($mode eq "train" && !($srcFile && $tgtFile && $srcDep && $alignFile)) {
	die "-mode train -s sourceFile -t targetFile -d sourceDep -a alignFile\n";
}
elsif($mode eq "test" && !($srcFile && $tgtFile && $srcDep)) {
	die "-mode train -s sourceFile -d sourceDep -a alignFile\n";
}
elsif($mode eq "decode" && !($srcFile && $srcDep)) {
	die "-mode decode -s sourceFile -d sourceDep\n";
}

loadfeaturefiles::load_featurefiles;

#my($Corpus) = new VaaneeDecode($srcFile,$tgtFile,$srcDep,$alignFile);
my($Corpus) = new VaaneeDecodeFastPerm($srcFile,$tgtFile,$srcDep,$alignFile);

#$Corpus->print();

my($Parameters) = new Parameters;

if($mode eq "list") {
	$Corpus->printLists();
}
elsif($mode eq "train")
{
	print STDERR "Initialize parameters\n";
	if($C{'paramFile'} eq "")
	{
		print STDERR "Initializing with 0.01\n";
	       	$Parameters->initialize($Corpus,0.01); 
	}
	elsif($C{'prevIter'}==0)
	{
	       	$Parameters->load($C{'paramFile'}); 
	}
	else
	{
	       	$Parameters->load($C{'paramFileUAvg'}); 
	}

	$Parameters->list();

	print STDERR "Training"."\n";
	$Corpus->train($Parameters,$dictFile,$numDict);

	print STDERR "Training DOne"."\n";
	#print STDERR "List parameters\n";
	#$Parameters->list();
}
elsif($mode eq "test")
{
	print STDERR "Loading parameters $C{'paramFile'}\n";
	$Parameters->load($C{'paramFile'});

	#print STDERR "Listing parameters\n";
	#$Parameters->list();

	$Corpus->test($Parameters,$dictFile,$numDict);
}
elsif($mode eq "decode") 
{
	$Parameters->load($C{'paramFile'});
	
	$Corpus->decodeText($Parameters);
}

loadfeaturefiles::unload_featurefiles;

